<?php
namespace frontend\controllers;

use common\models\LoginForm;
use common\models\User;
use common\models\Video;
use common\models\VideoType;
use frontend\models\Comment;
use Yii;
use yii\web\Controller;
use yii\web\ServerErrorHttpException;

class VideoController extends Controller
{
    public function actions()
    {
        return [
            'Kupload' => [
                'class' => 'pjkui\kindeditor\KindEditorAction',
            ]
        ];
    }

    public function actionIndex()
    {
        $this->layout = "home";
        return $this->render('index');
    }

    public function actionPartition()
    {
        $videoTypes = VideoType::find()->where(['pid' => '0'])->asArray()->all();
        $this->layout = "home";
        return $this->render('partition',['videoTypes' => $videoTypes]);
    }

    public function actionVideo()
    {
        $this->layout = "home";
        $videoTypeId = Yii::$app->request->get();
        $videoTyle = VideoType::findOne(['id' =>$videoTypeId ]);
        //如果是顶级分类找出所有子分类
        if ($videoTyle->pid == '0'){
            $Subclass = VideoType::find()->select('id,name')->where(['pid' => $videoTypeId])->asArray()->all();
            $ids = array_column($Subclass,'id');
            $SubclassNames = array_column($Subclass,'name','id');
            $ids[] = $videoTypeId['id'];
            $videos = Video::find()->select('video_title,description,cover_img,id,watch_number')->where(['in', 'type', $ids])->asArray()->all();
        }else{
            $videos = Video::find()->select('video_title,description,cover_img,id,watch_number')->where(['type' => $videoTypeId])->asArray()->all();
            $SubclassNames = "";
        }
//        echo "<pre/>";
//        var_dump($videos);die;
        return $this->render('video',['videos' => $videos,'SubclassNames' => $SubclassNames]);
    }

    public function actionDetails()
    {
        //判断用户是否点赞或者踩
        $userinfo = Yii::$app->user->identity;
        if (empty($userinfo->is_vote)){
            $is_vote = true;
        }else{
            $is_vote = false;
        }
        $this->layout = "home";
        $videoTypeId = Yii::$app->request->get();
        $videos = Video::find()->alias("a")->select('a.id,b.video_url,video_title,description,cover_img,watch_number,like_number,step_number')->where(['a.id' => $videoTypeId['video_id']])->leftJoin('video_user AS b','a.id = b.video_id')->one();
        if (empty($videos->watch_number)){
            $videos->watch_number = 0;
        }
        $videos->watch_number = ++$videos->watch_number;
        $videos->save(false);
        $comment = new Comment();
        return $this->render('video_details',['videos' => $videos,'is_vote' => $is_vote,'comment' => $comment]);
    }

    public function actionLike()
    {
        $videos = Yii::$app->request->post();
        $video = Video::find()->where(['id' => $videos['id']])->one();
        if ($videos['type'] == 1){
            $video->like_number = ++$video->like_number;
            $video->save(false);
        }else{
            $video->step_number = ++$video->step_number;
            $video->save(false);
        }
        $userinfo = Yii::$app->user->identity;
        $user = User::find()->where(['id' => $userinfo->id])->one();
        $user->is_vote = 1;
        $user->save(false);
    }
}